package com.mrjy.common.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.mrjy.common.entity.PolyInfo;
import org.apache.ibatis.annotations.Case;

/**
 * 保利威视 Util类
 *
 * @author csshui
 */
public class PolyvUtil {
    /**
     * xxl
     */
//    private static String READTOKEN = "UluKFDIPv5-1L3etwXESt-ZslH8ZtL8N";
    /**
     * mrjy
     */
    private static String READTOKEN = "cb85f8c7-f429-4a97-b851-7b399499e2de";

    /**
     * javin lyon
     */
    // private static String READTOKEN = "38fe6df7-cedc-45e2-90e5-eb2bce4541ac";

	/**
	 * 通过视频id，获取视频播放路径
	 * @param vid 视频id
	 * @return
	 */
	public static Object queryUrlByVid(String vid){
		//Map<Object,Object> map = new HashMap<Object,Object>();
        if (StringUtils.isBlank(vid)) {
            return null;
        }
        String url = "http://v.polyv.net/uc/services/rest";
		Map<String, String> params = new HashMap<String, String>();
		params.put("method", "getById");
		params.put("readtoken", READTOKEN);
        params.put("vid", vid);
        String data = HttpUtil.doPost(url, params);

		JSONObject jsonObj = JSONObject.fromObject(data);
        if (jsonObj.get("data") != null && !jsonObj.get("data").equals("null")) {
            PolyInfo polyInfo = new PolyInfo();
			try {
				List<Map<String,Object>> list = (List<Map<String,Object>>)jsonObj.get("data");
				Map<String, Object> jsonMap = list.get(0);
				//List<String> imglList = JSONObject.fromObject(jsonMap).getJSONArray("images");
				//List<String> imghList = JSONObject.fromObject(jsonMap).getJSONArray("images_b");
                polyInfo.setMpvFlag(Integer.parseInt(jsonMap.get("df") + ""));
                polyInfo.setSeed(Integer.parseInt(jsonMap.get("seed") + ""));
                if (polyInfo.getSeed() == 0) {
                    polyInfo.setMpvl(jsonMap.get("mp4_1") + "");
                    polyInfo.setMpvm(jsonMap.get("mp4_2") + "");
                    polyInfo.setMpvh(jsonMap.get("mp4_3") + "");
                } else {
                    JSONArray hls = (JSONArray) jsonMap.get("hls");
                    switch (hls.size()) {
                        case 3:
                            polyInfo.setHls_h(hls.get(2) + "");

                        case 2:
                            polyInfo.setHls_m(hls.get(1) + "");

                        case 1:
                            polyInfo.setHls_l(hls.get(0) + "");

                    }
                }
                //polyInfo.setImgl(imglList.get(0));
                //polyInfo.setImgh(imghList.get(0));
				String title = String.valueOf(jsonMap.get("title"));
                Integer source_filesize1 = (int) jsonMap.get("source_filesize");
                float source_filesize = source_filesize1 / 1024.0f / 1024.0f;
                DecimalFormat df = new DecimalFormat("#.00");
                polyInfo.setMp3(jsonMap.get("mp4")+"");
                polyInfo.setSourceFilesize(String.valueOf(df.format(source_filesize)));
                polyInfo.setTitle(title);
                polyInfo.setDuration(jsonMap.get("duration")+"");
                if (StringUtils.isNotBlank(title) && title.indexOf(".mp3") > 0) {
                    polyInfo.setType("mp3");
                } else {
                    polyInfo.setType("mp4");
                }
			} catch (Exception e) {
				e.printStackTrace();
			}
			return polyInfo;
		}else{
			return null;
		}
	}

    /**
     * 获取单个视频信息
     *
     * @param vid 视频id
     * @return
     */
    public static List<PolyInfo> queryVideoMsgByVids(String[] vids) {
        //Map<Object,Object> map = new HashMap<Object,Object>();
        String url = "http://v.polyv.net/uc/services/rest";
        List<PolyInfo> list = new ArrayList<>();
        for (String vid : vids) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("method", "getById");
            params.put("readtoken", READTOKEN);
            params.put("vid", vid);
            String data = HttpUtil.doPost(url, params);

            JSONObject jsonObj = JSONObject.fromObject(data);
            if (jsonObj.get("data") != null && !jsonObj.get("data").equals("null")) {
                PolyInfo polyInfo = new PolyInfo();
                try {
                    List<Map<String, Object>> json_list = (List<Map<String, Object>>) jsonObj.get("data");
                    Map<String, Object> stringStringMap = json_list.get(0);
                    String title = String.valueOf(stringStringMap.get("title"));
                    Integer source_filesize1 = (int) stringStringMap.get("source_filesize");
                    float source_filesize = source_filesize1 / 1024.0f / 1024.0f;
                    DecimalFormat df = new DecimalFormat("#.00");
                    polyInfo.setMpvh(stringStringMap.get("mp4").toString());
                    polyInfo.setSourceFilesize(String.valueOf(df.format(source_filesize)));
                    polyInfo.setTitle(title);
                    polyInfo.setDuration(stringStringMap.get("duration").toString());

                    if (polyInfo.getMpvh().indexOf(".mp3") > 0) {
                        polyInfo.setType("mp3");
                    } else {
                        polyInfo.setType("mp4");
                    }
                    list.add(polyInfo);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return list;
    }


}
